/**
 * 
 */
package kr.co.insoft.publicmesh.controller.template.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.core.model.template.application.ApplicationEntityModel;
import kr.co.insoft.core.model.template.application.EventEntityModel;
import kr.co.insoft.core.service.template.application.ApplicationService;
import kr.co.insoft.core.service.template.application.EventService;
import kr.co.insoft.core.service.template.script.ScriptService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "template/event")
public class EventController {

	private static final Logger logger = LoggerFactory
			.getLogger(EventController.class);
	
	final String TILES_PAGE_PREFIX = "tiles/template/application/event";
	final String SINGLE_PAGE_PREFIX = "template/application/event";

	@Autowired
	EventService eventService;
	
	@Autowired
	ScriptService scriptService;
	
	@Autowired
	ApplicationService applicationService;

	/**
	 * Event 등록화면
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "applicationid", required = true) String applicationid,
			@RequestParam(value = "ip", defaultValue = "Y", required = false) String ip)  throws ServiceException  {
		
		ApplicationEntityModel application = 
				applicationService.getApplication(applicationid);
		
		EventEntityModel eventEntityModel = new EventEntityModel();
		eventEntityModel.setApplicationid(application.getApplicationid());
		model.addAttribute("scriptList", scriptService.getList());
		model.addAttribute("eventEntityModel", eventEntityModel);
		
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/createForm";
		
		return TILES_PAGE_PREFIX + "/createForm";
	}

	/**
	 * Event 등록
	 * 
	 * @param eventEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> createEvent(
			@ModelAttribute("eventEntityModel") EventEntityModel eventEntityModel,
			@RequestParam(value = "ip", defaultValue = "Y", required = false) String ip,
			final BindingResult bindingResult) throws Exception {
		try {
			if (bindingResult.hasErrors()) {
				return new ResponseEntity<Boolean>(false, HttpStatus.OK);
			}
			eventService.createEvent(eventEntityModel);
		
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Event 상세 화면
	 * 
	 * @param scriptid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String edit(
			Model model,
			@RequestParam("targetid") String eventid,
			@RequestParam(value = "ip", defaultValue = "Y", required = false) String ip)
			throws Exception {
		model.addAttribute("eventEntityModel", eventService.getEvent(eventid));
		model.addAttribute("scriptList", scriptService.getList());
		
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/edit";

		return TILES_PAGE_PREFIX + "/edit";
	}
	
	/**
	 * Event 수정
	 * 
	 * @param eventEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> edit(
			@ModelAttribute("eventEntityModel") EventEntityModel eventEntityModel) {		
		try {
			eventService.doEditEvent(eventEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Event 삭제
	 * 
	 * @param eventEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@RequestParam("eventid") String eventid) {		
		try {
			eventService.doRemoveEvent(eventid);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Event Name 중복 체크
	 * 
	 * @param ScriptEntityModel
	 * @return
	 */
	@RequestMapping(value = "isExistEventName", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> isExistEventName(
			@ModelAttribute("eventEntityModel") EventEntityModel eventEntityModel) {
		try {
			if (eventService.isExistEventName(eventEntityModel))
				return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(false, HttpStatus.OK);
	}
	
	/**
	 * Event 목록
	 * 
	 * @param scriptid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public @ResponseBody  List<EventEntityModel> list(
			Model model,
			@RequestParam("applicationid") String applicationid)
			throws Exception {
		
		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("applicationid", applicationid);
		
		return eventService.getList(param);
	}
}
